<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %> 
<%@ taglib uri="/WEB-INF/tld/ui.tld" prefix="ui" %> 
<%String path = request.getContextPath() ;%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<jsp:include page="../../import.jsp" flush="true" />
<script type="text/javascript" src="<%=path %>/resource/lib/plugins/ajaxfileupload.js"></script>
</head>
<body class="iframeBody">
<div class="Wrapper">
<div>
<div id="pageInTab" class="pageInTab">
	<div tabid="tabA" lselected="true">
		<div class="searchArea">
			<!-- 搜索表单开始 -->
			<form id="searchBar">
				<div class="fieldset1 clearFix">
					<div class="FXBR">
					<div class="fieldBox">
							<div class="label">承租人名称</div>
							<div class="field">
								<input type="text" id="cstNm" name="cstNm" class="ui-textbox" ligerui="width: 160" value="">
							</div>
						</div>
						<div class="fieldBox">
							<div class="label">合同号</div>
							<div class="field">
								<input type="text" id="contractNo" name="contractNo" class="ui-textbox" ligerui="width: 160" value="">
							</div>
						</div>
						<div class="fieldBox">
							<div class="label">支付表号</div>
							<div class="field">
								<input type="text" id="paySchNo" name="paySchNo" class="ui-textbox" ligerui="width: 160" value="">
							</div>
						</div>
						<!-- <div class="fieldBox">
							<div class="label">省公司</div>
							<div class="field">
								<input name="proCompany" id="proCompany" data-value=" " placeholder="--请选择--" ltype="combobox">
							</div>
						</div> -->
		            	<div class="fieldBox">
				             <div class="label" style="width: 60px;">事业部</div>
				                <div class="field">
				                    <select name="bussDepartment" id="bussDepartment" onchange="searchOrgTreeByOrg(this);" >
				                       <option value="" seleted>请选择</option>
				                       <c:forEach items="${OrgList}" var="orgCd">
				                        	<option value="${orgCd.key}">${orgCd.value}</option>
				                        </c:forEach>
				                    </select>
				             </div>
				        </div>
	            	   <div class="fieldBox col_50">
		                    <div class="label" style="width: 60px;">省公司</div>
		                    <div class="field">
		                        <input name="cstMgrOrgCd" id="cstMgrOrgCd" type="text" value="" placeholder="请选择" ltype="combobox">
		                    </div>
			           </div>
						<div class="fieldBox">
		                    <div class="label">结清时间</div>
		                    <div class="field">
		                        <input ligerui="width: 82" name="actPayTmB" id="actPayTmB" type="text" class="ui-datepicker" value="">
		                        -
		                        <input ligerui="width: 82" name="actPayTmE" id="actPayTmE" type="text" class="ui-datepicker" value="">
		                    </div>
	               	    </div>
	               	    <div class="fieldBox">
							<div class="label">结清状态</div>
							<div class="field">
								<select id="prdstscd"  name="prdstscd"  ligerui="width: 160">
									<option value="">请选择</option>
									<c:forEach items="${prdStsCdList}" var="item">
										<c:if test="${item.key=='S010301'||item.key=='S010401'||item.key=='S010501'||item.key=='S0106020101'||item.key=='S0106020201'||item.key=='S01060203010101'
										||item.key=='S0103'||item.key=='S0104'||item.key=='S0105'||item.key=='S01'}">    
										<option value="${item.key}">${item.value}</option>
										</c:if>
									</c:forEach>
								</select>
							</div>
						</div>
						<div class="fieldBox">
							<div class="label">移交状态</div>
							<div class="field">
								<select id="transferStatus"  name="transferStatus"  ligerui="width: 160">
									<option value="">请选择</option>
									<c:forEach items="${transferStateList}" var="item">
										<option value="${item.key}">${item.value}</option>
									</c:forEach>
								</select>
							</div>
						</div>
						<div class="fieldBox">
							<div class="label">客户经理</div>
							<div class="field">
								<input name="cstMgrNm" id="cstMgrNm" class="ui-textbox">
							</div>
						</div>
						<div class="fieldBox">
							<div class="label">移交分类</div>
							<div class="field">
								<select id="transferType"  name="transferType"  ligerui="width: 160">
									<option value="">请选择</option>
									<option value="2">正常借出结清</option>
									<option value="1">提前借出省公司保管</option>
								</select>
							</div>
						</div>
						
					</div>
					<div class="clearFix"></div>
					
					<!-- 表单按钮 -->
					<div class="btnsBox formBtns notRight">
						<span id="searchBtn" class="sq-button-round">查询</span>
						<span id="clearBtn"   class="sq-button-round">清空</span>
						<span id="applyTransfer"   class="sq-button-round">申请移交</span>
						<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
						<input id="upfile" name="upfile" style="width:150px" type="file">						
						<span onclick="batchTransfer()"  class="sq-button-round">上传批量移交合同资料</span>
						<span>
							<a style="color:blue;text-decoration:underline;" href="<%=path%>/resource/template/reffile/transferDemo.xls" />批量移交模板下载</a> 
		                </span>
						<input id="resetBtn" type="reset" value="重置" class="l-button l-button-reset Hide"/>
		            </div>
		            
				</div>
			</form>
			<!-- 搜索表单结束 -->
		</div>
		<!-- 列表显示块 -->
		<div id="ligergrid1" class="sq-grid"></div>
	</div>
	
</div>
</div>
</div>
<script type="text/javascript">
	var pageInApp={
		flag:false //tab页验证关闭
	};//to下一级子窗口 接口
	var pageInTab=null;//页签
	var loadedTabs=[];//页签
	var grid = null;
	//修改时数据初始值   
    var _bussCdUpdataValue = "";//事业部
    var _provCdUpdataValue = "";//省公司
  //记录第一次勾选移交分类状态
	var trType;
	
	
	$(function () {
		//initOrgTree();
		initOrgTree2();//注意要放在ligerForm之前加载
		$("#searchBar").ligerForm();//初始化表单
	    initGrid();//初始化列表
	    searchBtnEvent();//查询按钮事件
	    clearBtnEvent();//清空按钮事件
	    applyTransfer();//申请移交
	    
	});
	
	//查询参数
	function getSearchParams(){
		var form = new liger.get("searchBar");
		var data = form.getData();
		return data;
	}
	//定义选择行数据-存支付表号
	var paySchNos = new Array();
	//定义一个空的JSONArray{paySchNo paySchId  transferStatus  storageStatus}
	var transferArray = [];
	
	//初始化列表
	function initGrid(){
		$("#ligergrid1").ligerGrid({
			url:'<%=path%>/archive/arcTransferMain/initList.do',
			method:'get',
			parms:getSearchParams(),
			columns:[
				{ display: '承租人姓名', name: 'cstNm', minWidth: 100,
					render:function (rowdata, rowindex, value){
						return "<span title="+rowdata.cstNm+">"+rowdata.cstNm+"</span>";
					}
				},
				{ display: '合同号', name: 'contractNo', minWidth: 150,
					render:function (rowdata, rowindex, value){
						return "<span title="+rowdata.contractNo+">"+rowdata.contractNo+"</span>";
					}
				},
				{ display: '支付表号', name: 'paySchNo', minWidth: 160,
					render:function (rowdata, rowindex, value){
						return "<span title="+rowdata.paySchNo+">"+rowdata.paySchNo+"</span>";
					}
				},
				{ display: '结清时间', name: 'stlDt', minWidth: 50},
				{ display: '事业部', name: 'buOrgCdNm', minWidth: 60},
				{ display: '省公司', name: 'prOrgCdNm', minWidth: 90},
				{ display: '结清状态', name: 'prdStsCdNm', minWidth: 80,
					render:function (rowdata, rowindex, value){
						 var h="";
// 						 if(rowdata.prdStsCdNm=='正常'){
// 							 h='';
// 						 }else if(rowdata.prdStsCdNm=='已拖回'){
// 							 h='';
// 						 }else if(rowdata.prdStsCdNm=='拖回已转优车'){
// 							 h='';
// 						 }else{
							 h=rowdata.prdStsCdNm;
// 						 }
						 return h;
					 }
				},
				{ display: '纸质归档状态', name: 'storageStatusNm', minWidth: 40},
				{ display: '移交状态', name: 'transferStatusNm', minWidth: 30},
				{ display: '移交分类', name: 'transferType', minWidth: 100,
					 render:function (rowdata, rowindex, value){
						 var h="";
						 if(rowdata.transferType=='1'){
							 h='提前借出省公司保管';
						 }else if(rowdata.transferType=='2'){
							 h='正常借出结清';
						 }else{
							 if(rowdata.prdStsCd=='S01' || rowdata.prdStsCd=='S0103' || rowdata.prdStsCd=='S0104' || rowdata.prdStsCd=='S0105'){
								 h='提前借出省公司保管';
							 }else{
								 h='正常借出结清';
							 } 
						 }
						 return h;
					 }
				},
				{ display: '客户经理', name: 'cstMgrNm', minWidth: 30},
				{
				  display:'操作',minWidth: 120,align:'left',
				  render:function (rowdata, rowindex, value){
						var h="";
						 if(rowdata.transferStatus != '0'){
							 h+='<a class="sq-button-hasicon" href="javascript:detailInfo(';
				                h+="'"+rowdata.id+"'";
				                h+=",'"+rowdata.paySchNo+"'";
				                h+=")\"><i class='sqBtnIco_4'></i>查看</a>";
						 }else{
							 h+='<font class="sq-button-hasicon" color="c9c9c9"><i class="sqBtnIco_4"></i>查看</font>';
						 }
		                if(rowdata.transferStatus != '0'){
		                	h+='<a class="sq-button-hasicon" href="javascript:viewFlow(';
			                h+="'"+rowdata.paySchNo+"'";
			                h+=")\"><i class='sqBtnIco_4'></i>流程信息</a>";
		                }else{
		                	h+='<font class="sq-button-hasicon" color="c9c9c9"><i class="sqBtnIco_4"></i>流程信息</font>';
		                }
						return h;
					}
				},
			],
			pageSize:10,
			root:'rows',
			record:'total',
			hideLoadButton:false,
			width:'100%',
			height:'100%',
			heightDiff:-2,
			isScroll: true,
			checkbox:true,//列表是否有选择框
			rownumbers:true,//列表是否有行号
			//选择行事件
			onSelectRow: function(rowdata, rowid, rowobj){
				var paySchNo = "'"+rowdata.paySchNo+"'";
				var trObject={};//定义一个行对象
				trObject.paySchId=rowdata.paySchId;
				if(rowdata.transferStatus == '0' && (rowdata.storageStatus=='3'||rowdata.storageStatus=='4' || rowdata.eleStorageStatus=='3' || rowdata.eleStorageStatus=='4') ){
					
					trObject.transferStatus=rowdata.transferStatus;
					trObject.storageStatus=rowdata.storageStatus;
					trObject.paySchNo=rowdata.paySchNo;
					trObject.contractNo=rowdata.contractNo;
					trObject.eleStorageStatus=rowdata.eleStorageStatus;
	            	//重复不添加
	            	if(paySchNos.join().indexOf(paySchNo) == -1){
	            		paySchNos[paySchNos.length] = paySchNo;
	            		transferArray.push(trObject);
	            	}
	            	var lin;
	            	if(rowdata.prdStsCd=='S01' || rowdata.prdStsCd=='S0103' || rowdata.prdStsCd=='S0104' || rowdata.prdStsCd=='S0105'){
	            		lin=1;
	            	}else{
	            		lin=2;
	            	}
	            	//实现 移交分类不可混选功能
	            	if(null==trType || ''==trType){
	            		trType=rowdata.prdStsCd;
	            		if(trType=='S01' || trType=='S0103' || trType=='S0104' || trType=='S0105'){
	            			trType="1";
	            		}else{
	            			trType="2";
	            		} 
	            	}
            		var cuType=rowdata.prdStsCd;
	            	if(cuType=='S01' || cuType=='S0103' || cuType=='S0104' || cuType=='S0105'){
	            		cuType="1";
	            	}else{
	            		cuType="2";	
	            	}
	            	if(trType!=cuType){
	            		var manager = $("#ligergrid1").ligerGetGridManager();
						manager.unselect(rowdata);
						return false;
	            	}
					//end 实现移交分类不可混选功能
				}else{
					var manager = $("#ligergrid1").ligerGetGridManager();
					manager.unselect(rowdata);
					return false;
				}
			},
			//取消选择行事件
			onUnSelectRow : function(rowdata, rowid, rowobj){
				var paySchNo = "'"+rowdata.paySchNo+"'";
				var trObject={};//定义一个行对象
				trObject.paySchId=rowdata.paySchId;
				trObject.transferStatus=rowdata.transferStatus;
				trObject.storageStatus=rowdata.storageStatus;
				trObject.paySchNo=rowdata.paySchNo;
				trObject.contractNo=rowdata.contractNo;
				//删除已存数据
    			for(var i=0;i<paySchNos.length;i++){
    				if(paySchNos[i] == paySchNo){
    					paySchNos.splice(i, 1);
    					transferArray.pop(trObject);
    				}
    			}
    			
			},
			 onAfterShowData: f_onAfterShowData,
		});
		grid = liger.get("ligergrid1");
		grid.setHeight(100);
		//grid.options.isSingleCheck = true;//列表是否为单选
	}
	
	//加载默认选中行-JL-20181207
    function f_onAfterShowData(currentData){
    	if(currentData!=null){
	    	var arr = eval(currentData);
	    	//$.ligerDialog.alert("currentData="+JSON.stringify(arr));//注释
	        var rows = arr.rows;
            var manager = $("#ligergrid1").ligerGetGridManager();
	        for(var i=0;i<rows.length;i++){
	        	var paySchNo = "'"+rows[i].paySchNo+"'";
	        	for(var j=0;j<paySchNos.length;j++){
	        		//对比支付表号
	        		if(paySchNos[j] == paySchNo){
	            		manager.select(i);//此时会调用onSelectRow选中行事件
	            		break;//节省时间
		        	}
	        	}
	        }
    	}
    }
	//清空按钮事件
	function clearBtnEvent(){
		$("#clearBtn").click(function(event) {
			$("#resetBtn").click();  //重置 input-text
			var form = liger.get("searchBar");
			form.setData({  //重置select等特殊表单
				"prdstscd":"",
				"transferStatus":"",
				"bussDepartment":"",
				"proCompany":"",
				"actPayTmB":"",
				"actPayTmE":"",
				"cstMgrNm":"",
				"transferType":""
			});
			//searchOrgTree.clear();
			searchOrgTree2.clear();
		});
	}
	
	//查询按钮事件
	function searchBtnEvent(){
		$("#searchBtn").click(function(event) {
			var form = new liger.get("searchBar");
			var data = form.getData();
			//alert(JSON.stringify(data));
			for (name in data){
				grid.setParm(name,data[name]);
			};
			//searchOrgTreeByOrgNm(data);
			grid.reload();
		});
	}
	 //通过查询条件获取Tree数据
    function searchOrgTreeByOrg (obj) {
        var _url = '<%=path%>/warrant/warrantfilemanage/searchBuOrgTree.do';
        var params = {};
        params.cstMgrOrgCd=obj.value;
    	AjaxUtils.ajaxGet(_url, params, function(msg){
    		if(msg.success == 1){
  				searchOrgTree2.treeManager.setData(msg.data);
  				searchOrgTree2.clear();
  			}else{
  				//UIUtils.alertError(msg.info);
  				$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
  			}
    	 },false);
    }
	 
	//查看移交详情
	function detailInfo(id,paySchNo) {
		
		//回调刷新列表
		var param = '<%=path%>/archive/arcTransferMain/getView.do?flag=000&id=' + id +'&paySchNo='+paySchNo;
		
		Add_navTab('getView'+id,'查看详情',param, function () {
			
		});
	}
	//申请移交
	function arcApplyTransfer(id,paySchNo,isGrpBiz,prdStsCd,bizTypCd,buOrgCd,contractNo,cstId,cstNm,paySchId) {
		//1、回调刷新列表
		var param = '<%=path%>/archive/arcTransferMain/toApplyTransferFlow.do?paySchNo='+paySchNo+'&buOrgCd='+buOrgCd+'&bizTypCd='+bizTypCd+'&isGrpBiz='+isGrpBiz+'&prdStsCd='+prdStsCd+'&id='+id+'&contractNo='+contractNo+'&cstNm='+cstNm+'&cstId='+cstId+'&paySchId='+paySchId;
		Add_navTab('warrantFileManage'+paySchNo,'申请移交',param, function () {
			grid.reload();
		});
		
	}

	//查看流程
	function viewFlow(paySchNo){
		//回调刷新列表
		var param = '<%=path%>/archive/arcTransferMain/toViewFlow.do?paySchNo=' + paySchNo ;
		
		Add_navTab('toViewFlow'+paySchNo,'查看流程',param, function () {
			grid.reload();
		});
	}
	 //初始化查询条件中的下拉树
    var searchOrgTree2;
    function initOrgTree2(){
    	 searchOrgTree2 = $("#cstMgrOrgCd").ligerComboBox({
             selectBoxWidth: 240,
             selectBoxHeight: 150,
             treeLeafOnly: false, //限制只能选叶节点，父节点不能点击
             valueField: 'orgCd',
             textField: 'orgDispNm',
	    	 tree: {
	             ajaxType: 'get',
	             idFieldName:'orgCd',
	             textFieldName: 'orgDispNm',
	             checkbox: false,
	             parentIcon: "none",
	             childIcon: "none",
	             needCancel : false,
	         }
         });
    	 var _url = '<%=path%>/warrant/warrantfilemanage/searchBuOrgTree.do';
    	 AjaxUtils.ajaxGet(_url, {}, function(msg){
    		if(msg.success == 1){
  				searchOrgTree2.treeManager.setData(msg.data);
  				searchOrgTree2.clear();
  			}else{
  				//UIUtils.alertError(msg.info);
  				$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
  			}
    	 },false);
    }
    //新申请移交
	function applyTransfer(){
		$("#applyTransfer").click(function(event) {
			
			if(transferArray.length==0){
				$.ligerDialog.SQalert({content: "请至少勾选一条数据!",SQ_type: "error"});
				return ;
			}
			if(transferArray.length>30){
				$.ligerDialog.SQalert({content: "所选数据已超过30条,请核查一下!",SQ_type: "error"});
				return ;
			}
			console.log(transferArray);
			var strPay="";
			
			for(var i=0;i<transferArray.length;i++){
				
				if(transferArray[i].transferStatus == '0' && (transferArray[i].storageStatus=='3'||transferArray[i].storageStatus=='4' || transferArray[i].eleStorageStatus=='3' || transferArray[i].eleStorageStatus=='4') ){
					strPay=strPay+transferArray[i].paySchId+",";
				}else{
					$.ligerDialog.SQalert({content: "归档或移交状态不符合!!",SQ_type: "error"});
					return ;
				}
			} 
			if(strPay==null||strPay==undefined||strPay==''){
				$.ligerDialog.SQalert({content: "请至少勾选一条数据!",SQ_type: "error"});
				return ;
			}
			strPay = strPay.substring(0, strPay.lastIndexOf(','));  
			console.log(strPay);
			
			 var timestamp=new Date().getTime();
			var param = '<%=path%>/archive/arcTransferMain/toApplyTransferFlowPage.do?payIdList='+strPay;
			Add_navTab('applyTransfer'+timestamp,'申请移交',param, function () {
				grid.reload();
				var win = $(top.document.getElementById(top.tab.selectedTabId));
				 win.attr('src', win.attr('src'));
				 transferArray.length = 0;
			}); 
		});
	}
    //批量移交
    function batchTransfer(){
    	var wait_dialog=$.ligerDialog.SQalert({content: '上传中,请稍等...' ,SQ_type: "waitting" });
    	$.ajaxFileUpload({  
    	    url : "<%=path%>/archive/arcTransferMain/importBatchTransfer.do",  
    	    dataType : 'json',  
    	    data : {},
    	    secureuri : false,  
    	    fileElementId :'upfile',
    	    success : function(msg) { //服务器成功响应处理函数  
    	    	wait_dialog.close();
    	    	if(msg.success == 1){
		    		 if(msg.data == ""){
		    			 UIUtils.alertErrorHold1("上传的EXCEL支付表编号为空");
		    		 }else{
			    		 top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "success"});
			    		 //跳转到移交申请界面
			    		 var timestamp=new Date().getTime();
			    		 var payIdStr=msg.data;
			 			 var param = '<%=path%>/archive/arcTransferMain/toApplyTransferFlowPage.do?payIdList='+payIdStr;
			 			 Add_navTab('applyTransfer'+timestamp,'申请移交',param, function () {
			 				grid.reload();
			 				var win = $(top.document.getElementById(top.tab.selectedTabId));
			 				 win.attr('src', win.attr('src'));
			 				 transferArray.length = 0;
			 			}); 
		    		 }
   			 }else{
   					UIUtils.alertErrorHold1(msg.info);
   			 }
            },error:function (XMLHttpRequest, textStatus, errorThrown) { 
    			wait_dialog.close();
    			$.ligerDialog.SQalert({content: "网络错误，请稍后重试",SQ_type: "error"});
    		}   
        }); 
    }
</script>
</body>
</html>
